/*
    12.Write a program in C to read a string through keyboard and sort it using bubble sort.
    Test Data :
    Input number of strings :3
    Input string 3 :
    zero
    one
    two

    Expected Output :

    The strings appears after sorting : 
    one 
    two 
    zero
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STRINGS 256
#define MAX_STR_LEN 1024

char* getstr(char *str)
{
    char ch;
    char *p = str;
    while((ch = getchar()) != '\n'){
        *p = ch;
        p++;
    }
    *p = '\0';
    return str;
}

int main(void)
{
    char *strs[MAX_STRINGS];
    char *tmp;
    int i,j,n;
    printf("Input number of strings : ");
    scanf("%d", &n);
    fflush(stdin);
    printf("Input string %d : \n", n);    
    for(i = 0; i < n; i++){
        strs[i] = malloc(MAX_STR_LEN);
        getstr(strs[i]);
    }
    strs[i] = NULL;
    for(i = 0; i < n - 1; i++){
        for(j = 0; j < n - i - 1; j++){
            if(strcmp(strs[j], strs[j + 1]) > 0){
                tmp = strs[j];
                strs[j] = strs[j + 1];
                strs[j + 1] = tmp;
            }
        }
    }
    printf("The strings appears after sorting : \n");
    for(i = 0; i < n; i++){
        printf("%s\n", strs[i]);
    }
    return 0;
}

